<?php

namespace app\agent\model\order;

use app\common\facade\auth\Auth;
use app\model\MovieOrder;
use think\facade\Db;
use app\common\model\Export;

/**
 * 影票订单
 */
class Movie
{
    private $table = 'movie_order';

    /**
     * 列表.
     */
    public function getList($params)
    {
        $where = [];

        $agentIds = [];
        if (Auth::from() == FROM_APP_YMSH){  //有美生活对接的自动识别Agent
            $agentIds = Auth::allBindAgentIds();
        }else{
            $where[] = ['agent_id', '=', $params['agent']['id']];
            $agentIds[] = $params['agent']['id'];

        }
        $where[] = ['status', '=', 2];
        if (!empty($params['channel_id'])) {
            $where[] = ['channel_id', '=', $params['channel_id']];
        }
        $orderId = trim($params['order_id'] ?? '');
        if ($orderId) {
            $where[] = ['order_id', '=', $orderId];
        }
        if (!empty($params['supplier_status'])) {
            if ($params['supplier_status'] == 1) {// 待出票
                $where[] = ['supplier_status', 'in', [1, 2, 3]];
            } elseif ($params['supplier_status'] == 2) {// 已出票
                $where[] = ['supplier_status', 'in', [4, 5]];
            } elseif ($params['supplier_status'] == 3) {// 已取消
                $where[] = ['supplier_status', '=', 10];
            } elseif ($params['supplier_status'] == 4) { // 已结算
                $where[] = ['settle_status', '=', 3];
            }
        }
        if (!empty($params['created_at'])) {
            $params['created_at'][1] .= ' 23:59:59';
            $where[] = ['created_at', 'between time', $params['created_at']];
        }
        //结算时间
        if (!empty($params['settle_time'])) {
            $settle_time_search = [strtotime($params['settle_time'][0]), strtotime($params['settle_time'][1] .= ' 23:59:59')];
            $where[] = ['settle_time', 'between', $settle_time_search];
        }
        $paginate = [
            'page' => $params['page'] ?? 1,
            'list_rows' => $params['list_rows'] ?? 10,
        ];
        $channelList = Db::name('agent_channel')
            ->field('id, name')
            ->whereIn('agent_id', $agentIds)
            ->where('is_deleted', 0)
            ->order('name asc')
            ->select()
            ->toArray();
        $channel = array_column($channelList, 'name', 'id');

        $list = MovieOrder::query()
            ->withSearch(['app_id','merchant_id'],request()->all())
            ->withSearch(['app_id','merchant_id'],request()->all())
            ->append(['settle_time_text'])
            ->where($where)
            ->order('id desc');

        // 导出数据字段处理
        if (!empty($params['is_export'])) {
            $list = $list->select()->toArray();
            $res = [];
            $title = ['系统订单号', '电影名称', '放映时间', '订单金额', '结算价', '利润', '状态', '下单时间', '完成时间'];
            $statusMap = [
                4 => '已出票',
                5 => '已出票',
                10 => '已取消',
            ];
            foreach ($list as $v) {
                if ($v['finish_time']) {
                    $v['finish_time'] = date('Y-m-d H:i:s', $v['finish_time']);
                } else {
                    $v['finish_time'] = '';
                }
                $res[] = [
                    $v['order_id']."\t",
                    $v['film_name'],
                    $v['show_time'] . ' ~ ' . $v['end_time'],
                    fen2yuan($v['sale_price']).'',
                    fen2yuan($v['sale_price'] - $v['profit']).'',
                    fen2yuan($v['profit']).'',
                    $statusMap[$v['supplier_status']] ?? '出票中',
                    $v['created_at'],
                    $v['finish_time'],
                ];
            }
            unset($list);
            $export = new Export;
            $name = '影票订单';
            return $export->downloadExcel($title, $res, $name);
        }

        $list = $list->paginate($paginate)->toArray();
        if ($list['data']) {
            foreach ($list['data'] as $k => $v) {
                $list['data'][$k]['channel_name'] = $channel[$v['channel_id']] ?? '';
                $list['data'][$k]['order_id'] = strval($v['order_id']);
                if ($v['finish_time']) {
                    $list['data'][$k]['finish_time'] = date('Y-m-d H:i:s', $v['finish_time']);
                } else {
                    $list['data'][$k]['finish_time'] = '';
                }
            }
        }
        $list['channelList'] = $channelList;

        return success('操作成功', $list);
    }
}
